﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class CinemaManagementControl : UserControl
    {
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        public CinemaManagementControl()
        {
            InitializeComponent();
            initDataGridView();
        }

        private void initDataGridView()
        {
            x_gridCinema.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn IDColumn = new DataGridViewTextBoxColumn();
            IDColumn.DataPropertyName = "ID";
            IDColumn.HeaderText = "Cinema ID";

            DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
            nameColumn.DataPropertyName = "CinemaName";
            nameColumn.HeaderText = "Cinema Name";

            DataGridViewTextBoxColumn screen1Column = new DataGridViewTextBoxColumn();
            screen1Column.DataPropertyName = "Screen1";
            screen1Column.HeaderText = "Screen 1";

            DataGridViewTextBoxColumn screen2Column = new DataGridViewTextBoxColumn();
            screen2Column.DataPropertyName = "Screen2";
            screen2Column.HeaderText = "Screen 2";

            DataGridViewTextBoxColumn screen3Column = new DataGridViewTextBoxColumn();
            screen3Column.DataPropertyName = "Screen3";
            screen3Column.HeaderText = "Screen 3";

            DataGridViewTextBoxColumn screen4Column = new DataGridViewTextBoxColumn();
            screen4Column.DataPropertyName = "Screen4";
            screen4Column.HeaderText = "Screen 4";

            DataGridViewTextBoxColumn screen5Column = new DataGridViewTextBoxColumn();
            screen5Column.DataPropertyName = "Screen5";
            screen5Column.HeaderText = "Screen 5";

            x_gridCinema.Columns.Add(IDColumn);
            x_gridCinema.Columns.Add(nameColumn);
            x_gridCinema.Columns.Add(screen1Column);
            x_gridCinema.Columns.Add(screen2Column);
            x_gridCinema.Columns.Add(screen3Column);
            x_gridCinema.Columns.Add(screen4Column);
            x_gridCinema.Columns.Add(screen5Column);
        }
        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }
        private void CinemaManagementControl_Load(object sender, EventArgs e)
        {
            x_gridCinema.DataSource = ListCinema;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int cinemaID = int.Parse(txtCinemaID.Text);
            string cinemaName = txtCinemaName.Text;
            ReportDataSet.tblCinemaDataTable t = new ReportDataSet.tblCinemaDataTable();

            EnumerableRowCollection<ReportDataSet.tblCinemaRow> enum1 = x_tblCinemaTableAdapter.GetData()
                .Where(kvp => kvp.ID == cinemaID);
            if (enum1 == null || enum1.Count() == 0)
            {
                x_tblCinemaTableAdapter.Insert(cinemaID, cinemaName, 1, 1, 1, 1, 1);
            }
            else
            {
                ReportDataSet.tblCinemaRow a1 = enum1.First();
                a1.CinemaName = cinemaName;
                x_tblCinemaTableAdapter.Update(a1);
            }
            ListCinema = null;
            x_gridCinema.DataSource = ListCinema;
        }

        private void x_gridCinema_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                ReportDataSet.tblCinemaRow row = ListCinema[e.RowIndex];
                txtCinemaID.Text = row.ID.ToString();
                txtCinemaID.ReadOnly = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtCinemaID.Text = "";
            txtCinemaID.ReadOnly = false;

        }
    }
}
